<?php

namespace dexcedu;

use PDO;

class Dexcpro
{
  // 模型对象
  protected $model;

  // 控制器类实例时,要确保模型和视图对象可用
  public function __construct($model)
  {
    $this->model = $model;
  }

  //这个忘了
  public function index()
  {
    header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With,X-PINGOTHER,Content-Type');
    header('Access-Control-Allow-Methods:POST,GET,OPTIONS');
    header("access-control-allow-origin: *");
    $array = ['*', 'token_list', '*', 9];
    $integral = $this->model->fetch($array);
  }

  //   代币列表
  public function tokenlist()
  {
    //   允许跨域
    header("access-control-allow-origin: *");
    $data = json_decode(file_get_contents('php://input'), true);
    $array = ['*', 'token_list', 'chain' . '=' . "'" . $data  . "'", 9];
    $integral = $this->model->fetch($array);
    echo json_encode([$integral]);
  }
  //   代币搜索
  public function TokenSearch()
  {
    header("access-control-allow-origin: *");
    $data = json_decode(file_get_contents('php://input'), true);
    $array = ['*', 'token_list', 'chain=' . "'" . $data[0]  . "'" . ' AND name LIKE' . "'%" . strtolower($data[1]) . "%'", 9];
    $integral = $this->model->fetch($array);
    $sql = sprintf('SELECT %s FROM %s WHERE %s', $array[0], $array[1], $array[2]);
    echo json_encode([$integral]);
  }
  // 获取交易所列表
  public function ExchangeList()
  {
    //   允许跨域
    header("access-control-allow-origin: *");
    $data = json_decode(file_get_contents('php://input'), true);
    $array = ['*', 'exchange_list', 'chainid' . '=' . "'" . $data  . "'", 9];
    $integral = $this->model->fetch($array);
    echo json_encode([$integral]);
  }
  //   获取网络 ETH
  public function Getnetworkrpc()
  {
    //   允许跨域
    header("access-control-allow-origin: *");
    $data = json_decode(file_get_contents('php://input'), true);
    $array = ['*', 'net_workrpc', '*', 9];
    $integral = $this->model->fetch($array);
    $array = ['*', 'token_list', 'ETH' . '=' . "'" . 1  . "'", 9];
    $ETH = $this->model->fetch($array);
    $array = ['*', 'dexc_contract', '*', 9];
    $dexc = $this->model->fetch($array);
    $array = ['*', 'dexc_agent', 'name' . '=' . "'" . $data  . "'", 1];
    $agentaddress = $this->model->fetch($array);
    echo json_encode([$integral, $ETH, $dexc, $agentaddress]);
  }
  // 获取某个表的全部
  public function presalelist()
  {
    //   允许跨域
    header("access-control-allow-origin: *");
    $data = json_decode(file_get_contents('php://input'), true);
    $array = ['*', $data, '*', 9];
    $sql = sprintf('SELECT %s FROM %s WHERE %s', $array[0], $array[1], $array[2]);
    $integral = $this->model->fetch($array);
    echo json_encode([$integral]);
  }
  // 获取代币机制列表
  public function mechanism()
  {
    //   允许跨域
    header("access-control-allow-origin: *");
    $data = json_decode(file_get_contents('php://input'), true);
    $array = ['*', 'token_mechanism', 'chanid' . '=' . "'" . $data  . "'", 9];
    $integral = $this->model->fetch($array);
    echo json_encode([$integral]);
  }
  public function mechanismid()
  {
    //   允许跨域
    header("access-control-allow-origin: *");
    $data = json_decode(file_get_contents('php://input'), true);
    $array = ['*', 'token_tutorial', 'MechanismID' . '=' . "'" . $data  . "'", 9];
    $integral = $this->model->fetch($array);
    echo json_encode([$integral]);
  }

  public function useronlinestatus()
  {
    header("access-control-allow-origin: *");
    $data = json_decode(file_get_contents('php://input'), true);
    // 查询
    //
    $call = '状态';

    $array = ['*', 'user_loginstatus', 'mac=' . "'" . $data['mac']  . "'", 1];
    $integral = $this->model->fetch($array);
    if ($integral) {
      // xiugai
      // 修改存在一个问题  如果用户已经登录 那么是不需要修改登录时间的
      if ($data['type'] == 'login') {
        // 登录 需要更新 所有信息
        $array = ['user_loginstatus', 'timeonline =' . "'" . $data['timeonline']   . "'," . 'lastonlinetime =' . "'" . $data['lastonlinetime']   . "'," . 'acting =' . "'" . $data['acting']   . "'," . 'LoginIP =' . "'" . $_SERVER['REMOTE_ADDR']   . "'", 'mac=' . "'" . $data['mac'] . "'"];
        $par  = $this->model->changedata($array);
        $call = '登录';
      } else if ($data['type'] == 'renew') {
        // 只更新最后上线时间 不要更新上线时间
        $array = ['user_loginstatus', 'lastonlinetime =' . "'" . $data['lastonlinetime']   . "'", 'mac=' . "'" . $data['mac'] . "'"];
        $par  = $this->model->changedata($array);
        $call .= '更新';
      }
    } else {
      // 添加
      $array = ['user_loginstatus', 'mac=' . "'" . $data['mac'] . "'," . 'timeonline =' . "'" . $data['timeonline']   . "'," . 'acting =' . "'" . $data['acting']   . "'," . 'lastonlinetime =' . "'" . $data['lastonlinetime']   . "'," . 'LoginIP =' . "'" . $_SERVER['REMOTE_ADDR']   . "'"];
      $arr =     $this->model->adddata($array);
      $call .= '首次登录';
    }



    echo json_encode([$call, date("Y-m-d H:i:s", $data['lastonlinetime'])]);
  }
}
